package com.flyduck.system.service;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.flyduck.common.exception.BusinessException;
import com.flyduck.common.util.*;
import com.flyduck.system.dao.SysMenuDao;
import com.flyduck.system.dao.SysUserDao;
import com.flyduck.system.enums.MenuTypeEnum;
import com.flyduck.system.exception.SystemExceptionCode;
import com.flyduck.system.vo.menu.*;
import com.flyduck.system.vo.menu.trans.*;
import com.google.common.collect.Lists;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import com.flyduck.system.entity.SysMenuEntity;

import java.util.Date;
import java.util.List;

@Service
public class SysMenuService {

    @Autowired
    private SysMenuDao sysMenuDao;
    @Autowired
    private SysUserDao sysUserDao;
    @Autowired
    private TokenUtil tokenUtil;

    /**
     * 获取菜单树
     *
     * @param reqVo
     * @return
     */
    public List<SysMenuTreeRespVO> getMenuTreeList(SysMenuTreeReqVO reqVo) {
        List<SysMenuEntity> menuList = getMenuList(reqVo);
        List<SysMenuTreeRespVO> sysDeptTreeRespVOS = Lists.transform(menuList, new SysMenuEntityToSysMenuTreeRespVO());
        List<SysMenuTreeRespVO> menuTree = null;
        menuTree = TreeUtil.toTree(SysMenuTreeRespVO.class, sysDeptTreeRespVOS);
        return menuTree;
    }


    private List<SysMenuEntity> getMenuList(SysMenuTreeReqVO reqVo) {
        QueryWrapper<SysMenuEntity> wrapper = getMenuQueryWrapper(reqVo);
        return sysMenuDao.list(wrapper);
    }

    private QueryWrapper<SysMenuEntity> getMenuQueryWrapper(SysMenuTreeReqVO reqVo) {
        QueryWrapper<SysMenuEntity> wrapper = new QueryWrapper<>();
        if (StringUtil.isNotBlank(reqVo.getMenuName())) {
            wrapper.like("menu_name", reqVo.getMenuName());
        }
        return wrapper;
    }

    /**
     * 新增菜单
     *
     * @param reqVo
     * @return
     */
    public boolean addMenu(SysMenuAddReqVO reqVo) {
        SysMenuEntity menuEntity = new SysMenuAddReqVOToSysMenuEntity().apply(reqVo);
        if (MenuTypeEnum.CONTENTS.getValue().equals(reqVo.getMenuType())) {
            menuEntity.setComponent("/home");
            menuEntity.setPath("/home");
        }
        menuEntity.setId(IdUtil.getId());
        menuEntity.setCreateName(tokenUtil.getLoginInfo().getUserInfo().getNickName());
        menuEntity.setCreateTime(new Date());
        menuEntity.setUpdateName(tokenUtil.getLoginInfo().getUserInfo().getNickName());
        menuEntity.setUpdateTime(new Date());
        return sysMenuDao.save(menuEntity);
    }

    /**
     * 获取菜单详情
     *
     * @param id
     * @return
     */
    public SysMenuDetailRespVO getMenuDetail(Long id) {
        SysMenuEntity sysMenuEntity = sysMenuDao.getById(id);
        return new SysMenuEntityToSysMenuDetailRespVO().apply(sysMenuEntity);
    }

    /**
     * 修改菜单
     *
     * @param reqVo
     * @return
     */
    public boolean updateMenu(SysMenuUpdateReqVO reqVo) {
        SysMenuEntity sysMenuEntity = new SysMenuUpdateReqVOToSysMenuEntity().apply(reqVo);
        sysMenuEntity.setUpdateName(tokenUtil.getLoginInfo().getUserInfo().getNickName());
        sysMenuEntity.setUpdateTime(new Date());
        return sysMenuDao.updateById(sysMenuEntity);
    }

    /**
     * 删除菜单
     *
     * @param id
     * @return
     */
    public boolean deleteMenu(Long id) {
        List<SysMenuEntity> childrenDept = sysMenuDao.findByParentId(id);
        if (CollectionUtil.isNotEmpty(childrenDept)) {
            throw new BusinessException(SystemExceptionCode.EXIST_CHILDREN_MENUS);
        }
        return sysMenuDao.removeById(id);
    }


    /**
     * 获取菜单下拉树
     *
     * @return
     */
    public List<SysMenuTreeSelectRespVO> getMenuTreeSelect() {
        List<SysMenuEntity> menuList = sysMenuDao.getAllMenus();
        List<SysMenuTreeSelectRespVO> menuTreeSelectRespVOList = Lists.transform(menuList, new SysMenuEntityToSysMenuTreeSelectRespVO());
        List<SysMenuTreeSelectRespVO> menuTree = null;
        try {
            menuTree = TreeUtil.toTree(SysMenuTreeSelectRespVO.class, menuTreeSelectRespVOList);
        } catch (Exception e) {
            e.printStackTrace();
        }
        return menuTree;
    }

}
